/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Redias Library App for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.libraryapp.model;

import java.util.HashMap;
import java.util.Map;

import android.os.Parcel;
import android.os.Parcelable;
import dk.redia.lib.log.Log;
import dk.redia.lib.xmlrpc.XmlRpcResultUtils;

public class SearchResultDataItem implements Parcelable {

	private static final String logTag = "SearchResultDataItem";
	
	private String title;
	private String date;
	private String type;
	private String identifier;
	private String abstractText;
	private String creator;
	private String coverId;
	private Map<String, SearchResultDataItem> children;
	private ObjectExtras oe;
	
	public SearchResultDataItem() {
		children = new HashMap<String, SearchResultDataItem>();
		title = "";
		date = "";
		type = "";
		identifier ="";
		abstractText = "";
		creator = "";
		coverId = "";
	}
	
	public static SearchResultDataItem fromMap(Map<String, Object> map) {
		SearchResultDataItem srdi = new SearchResultDataItem();
		if (map.containsKey("title")) srdi.setTitle((String)map.get("title"));
		if (map.containsKey("date")) srdi.setDate((String)map.get("date"));
		if (map.containsKey("type")) srdi.setType((String)map.get("type"));
		if (map.containsKey("identifier"))srdi.setIdentifier((String)map.get("identifier"));
		if (map.containsKey("abstract")) srdi.setAbstract((String)map.get("abstract"));
		if (map.containsKey("creator")) srdi.setCreator((String)map.get("creator"));
		if (map.containsKey("coverId")) srdi.setCoverId((String)map.get("coverId"));
		Map<String, Object> children = XmlRpcResultUtils.unpackStruct(map.get("children"));
		srdi.children = new HashMap<String, SearchResultDataItem>();
		if (children!=null) {
			for (String childObjId : children.keySet()) {
				Object childObj = children.get(childObjId);
				srdi.children.put(childObjId, SearchResultDataItem.fromMap(XmlRpcResultUtils.unpackStruct(childObj)));
			}
		} else {
			Log.w(SearchResultDataItem.logTag, "Failed to parse children array");
		}
		return srdi;
	}
	
	public boolean hasValidIdentifier() {
		return (this.identifier!=null && this.identifier.length()>0);
	}
	
	public boolean hasValidCoverId() {
		return (this.coverId!=null && this.coverId.length()>0);
	}
	
	public String toString() {
		return "{SearchResultDataItem: title=" + title + "}";
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getIdentifier() {
		return identifier;
	}

	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	public String getAbstract() {
		return abstractText;
	}

	public void setAbstract(String shortAbstract) {
		this.abstractText = shortAbstract;
	}

	public int describeContents() {
		return 0;
	}

	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(title);
		dest.writeString(date);
		dest.writeString(creator);
		dest.writeString(type);
		dest.writeString(identifier);
		dest.writeString(abstractText);
		dest.writeString(coverId);
		dest.writeParcelable(oe, 0);		
		dest.writeInt(children.size());
		for (String id : children.keySet()) {
			dest.writeString(id);
			dest.writeParcelable(children.get(id), 0);
		}
	}
	
	public String getCoverId() {
		return coverId;
	}

	public void setCoverId(String coverId) {
		this.coverId = coverId;
	}
	
	public Map<String, SearchResultDataItem> getChildren() {
		return children;
	}

	public static final Parcelable.Creator<SearchResultDataItem> CREATOR = new Parcelable.Creator<SearchResultDataItem>() {

		public SearchResultDataItem createFromParcel(Parcel source) {
			SearchResultDataItem srdi = new SearchResultDataItem();
			srdi.title = source.readString();
			srdi.date = source.readString();
			srdi.creator = source.readString();
			srdi.type = source.readString();
			srdi.identifier = source.readString();
			srdi.abstractText = source.readString();
			srdi.coverId = source.readString();
			srdi.oe = (ObjectExtras)source.readParcelable(ObjectExtras.class.getClassLoader());
			int childCount = source.readInt();
			for (int i=0;i<childCount;i++) {
				String childId = source.readString();
				SearchResultDataItem childSrdi = (SearchResultDataItem)source.readParcelable(SearchResultDataItem.class.getClassLoader());
				srdi.children.put(childId, childSrdi);
			}
			
			return srdi;
		}

		public SearchResultDataItem[] newArray(int size) {
			return new SearchResultDataItem[size];
		}
		
	};

	public void setObjectExtras(ObjectExtras oe) {
		this.oe = oe;
	}
	
	public ObjectExtras getObjectExtras() {
		return this.oe;
	}
}
